﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GetData_Software();

            GetData_WebSite();

            GetData_BusinessTag();
        }

    }

    public string GetIsOk(string v)
    {
        if (v == "0")
            return "未知";
        else if (v == "1")
            return "是";
        else if (v == "2")
            return "否";
        return "";
    }



    public string GetClickUrl(string id)
    {
        return string.Format("window.open('Default.aspx?action=edit&id={0}','','toolbar=no');", id);
    }

    #region WebSite 表格操作

    protected void gvwWebSite_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("lkbDelete") as LinkButton;
            if (lb.Text == "删除")
            {
                lb.Attributes.Add("onclick", "return confirm('确定要删除吗？')");
            }

        }
    }
    protected void gvwWebSite_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvwWebSite.EditIndex = e.NewEditIndex;
        GetData_WebSite();
    }//处于编辑状态
    protected void gvwWebSite_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvwWebSite.EditIndex = -1;

    }//取消编辑
    protected void gvwWebSite_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        DataEntity.T_WebSite WebSite = new DataEntity.T_WebSite();

        WebSite.WebSite_Name = (gvwWebSite.Rows[e.RowIndex].FindControl("txtWebSite_Name") as TextBox).Text;
        WebSite.WebSite_Address = (gvwWebSite.Rows[e.RowIndex].FindControl("txtWebSite_Address") as TextBox).Text;
        WebSite.WebSite_Rating = Convert.ToInt32((gvwWebSite.Rows[e.RowIndex].FindControl("ddlWebSiteRating") as DropDownList).SelectedValue);

        if (WebSite.WebSite_Name == string.Empty || WebSite.WebSite_Address == string.Empty)
        {
            Response.Write("<script>alert('网站名称或地址不能为空！')</script>");
        }
        else
        {

            DataAccess.Operate.DA_WebSite_Operate WebSite_Operate = new DataAccess.Operate.DA_WebSite_Operate();
            WebSite.WebSite_ID = new Guid(gvwWebSite.DataKeys[e.RowIndex].Value.ToString());
            if (WebSite_Operate.Update(WebSite))
            {
                Response.Write("<script>alert('更新成功！')</script>");
                gvwWebSite.EditIndex = -1;
                GetData_WebSite();
            }
            else
            {
                Response.Write("<script>alert('更新失败！')</script>");
            }


        }
    }

    protected void gvwWebSite_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int index = e.RowIndex;//获取当前行的索引
        object id = this.gvwWebSite.DataKeys[e.RowIndex].Value;
        DataAccess.Operate.DA_WebSite_Operate WebSite_Operate = new DataAccess.Operate.DA_WebSite_Operate();
        if (WebSite_Operate.Delete(id))
        {
            Response.Write("<script>alert('删除成功！')</script>");
            GetData_WebSite();
        }
        else
        {
            Response.Write("<script>alert('删除失败！')</script>");
        }

    }

    private void GetData_WebSite()
    {
        DataAccess.Operate.DA_WebSite_Operate WebSite_Operate = new DataAccess.Operate.DA_WebSite_Operate();
        DataSet ds = new DataSet();
        ds = WebSite_Operate.SelectAll();
        this.gvwWebSite.DataSource = ds.Tables[0];
        this.gvwWebSite.DataBind();
    }

    #endregion

    #region BusinessTag 表格操作

    protected void gvwBusinessTag_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("lkbDelete") as LinkButton;
            if (lb.Text == "删除")
            {
                lb.Attributes.Add("onclick", "return confirm('确定要删除吗？')");
            }

        }
    }
    protected void gvwBusinessTag_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvwBusinessTag.EditIndex = e.NewEditIndex;
        GetData_BusinessTag();
    }//处于编辑状态
    protected void gvwBusinessTag_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvwBusinessTag.EditIndex = -1;

    }//取消编辑
    protected void gvwBusinessTag_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        DataEntity.T_BusinessTag BusinessTag = new DataEntity.T_BusinessTag();

        BusinessTag.BusinessTag_Name = (gvwBusinessTag.Rows[e.RowIndex].FindControl("txtBusinessTag_Name") as TextBox).Text;

        if (BusinessTag.BusinessTag_Name == string.Empty)
        {
            Response.Write("<script>alert('网站名称或地址不能为空！')</script>");
        }
        else
        {

            DataAccess.Operate.DA_BusinessTag_Operate BusinessTag_Operate = new DataAccess.Operate.DA_BusinessTag_Operate();
            BusinessTag.BusinessTag_ID = new Guid(gvwBusinessTag.DataKeys[e.RowIndex].Value.ToString());
            if (BusinessTag_Operate.Update(BusinessTag))
            {
                Response.Write("<script>alert('更新成功！')</script>");
                gvwBusinessTag.EditIndex = -1;
                GetData_BusinessTag();
            }
            else
            {
                Response.Write("<script>alert('更新失败！')</script>");
            }


        }
    }

    protected void gvwBusinessTag_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int index = e.RowIndex;//获取当前行的索引
        object id = this.gvwBusinessTag.DataKeys[e.RowIndex].Value;
        DataAccess.Operate.DA_BusinessTag_Operate BusinessTag_Operate = new DataAccess.Operate.DA_BusinessTag_Operate();
        if (BusinessTag_Operate.Delete(id))
        {
            Response.Write("<script>alert('删除成功！')</script>");
            GetData_BusinessTag();
        }
        else
        {
            Response.Write("<script>alert('删除失败！')</script>");
        }

    }

    private void GetData_BusinessTag()
    {
        DataAccess.Operate.DA_BusinessTag_Operate BusinessTag_Operate = new DataAccess.Operate.DA_BusinessTag_Operate();
        DataSet ds = new DataSet();
        ds = BusinessTag_Operate.SelectAll();
        this.gvwBusinessTag.DataSource = ds.Tables[0];
        this.gvwBusinessTag.DataBind();
    }
    #endregion

    private void GetData_Software()
    {
        DataAccess.Operate.DA_SoftWare_Operate SoftWare_Operate = new DataAccess.Operate.DA_SoftWare_Operate();
        DataSet ds = new DataSet();
        ds = SoftWare_Operate.SelectAll();
        this.gvwSoftWare.DataSource = ds.Tables[0];
        this.gvwSoftWare.DataBind();
        this.lblCount.Text = "总数"+ds.Tables[0].Rows.Count;
    }

    protected void gvwSoftWare_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = e.Row.FindControl("LinkButton2") as LinkButton;
            if (lb.Text == "删除")
            {
                lb.Attributes.Add("onclick", "return confirm('确定要删除吗？')");
            }
        }

        e.Row.Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#ffffcc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.oldcolor;");
    }
    protected void gvwSoftWare_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;//获取当前行的索引
        object id = this.gvwSoftWare.DataKeys[e.RowIndex].Value;
        DataAccess.Operate.DA_SoftWare_Operate SoftWare_Operate = new DataAccess.Operate.DA_SoftWare_Operate();
        if (SoftWare_Operate.Delete(id))
        {
            Response.Write("<script>alert('删除成功！')</script>");
            GetData_Software();
        }
        else
        {
            Response.Write("<script>alert('删除失败！')</script>");
        }
    }
    protected void gvwSoftWare_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwSoftWare.PageIndex = e.NewPageIndex;
        GetData_Software();
    }
    protected void btnSarech_Click(object sender, EventArgs e)
    {
        DataAccess.Operate.DA_SofteWare_Inherit SofteWare_Inherit = new DataAccess.Operate.DA_SofteWare_Inherit();
        DataSet ds = new DataSet();
        ds = SofteWare_Inherit.GetSoftByName(txtSSoftWareName.Text.Trim());
        this.gvwSoftWare.DataSource = ds.Tables[0];
        this.gvwSoftWare.DataBind();
    }
}

